// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SurveyServiceImp.java

package com.iphaeton.lms.business.survey;

import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.survey.Survey;
import com.iphaeton.lms.pojo.survey.SurveyQuestion;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.sql.*;
import java.util.*;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;

// Referenced classes of package com.iphaeton.lms.business.survey:
//            ISurveyService

public class SurveyServiceImp extends PersistentServiceSupport
    implements ISurveyService
{

    public SurveyServiceImp()
    {
    }

    public Survey save(Survey sur)
        throws BusinessException
    {
        try
        {
            sur = (Survey)hbpservice.store(sur);
            hbpservice.commit();
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5C06\u8C03\u67E5\u95EE\u5377\u6301\u4E45\u5316\u5230\u6570\u636E\u5E93\u65F6\u51FA\u9519!");
        }
        return sur;
    }

    public void remove(String id)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(id, com.iphaeton.lms.pojo.survey.Survey.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u4ECE\u6570\u636E\u5E93\u4E2D\u5220\u9664\u8C03\u67E5\u95EE\u5377\u65F6\u51FA\u9519!");
        }
    }

    public void removeSurveyQuestion(String id)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(id, com.iphaeton.lms.pojo.survey.SurveyQuestion.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u4ECE\u6570\u636E\u5E93\u4E2D\u5220\u9664\u8C03\u67E5\u95EE\u5377\u9898\u76EE\u65F6\u51FA\u9519!");
        }
    }

    public Survey findSurveyById(String id)
        throws BusinessException
    {
        Survey sur = null;
        try
        {
            sur = (Survey)hbpservice.get(id, com.iphaeton.lms.pojo.survey.Survey.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u67E5\u8BE2\u8C03\u67E5\u95EE\u5377!");
        }
        return sur;
    }

    public SurveyQuestion findSurveyQuestionById(String id)
        throws BusinessException
    {
        SurveyQuestion sq = null;
        try
        {
            sq = (SurveyQuestion)hbpservice.get(id, com.iphaeton.lms.pojo.survey.SurveyQuestion.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u67E5\u8BE2\u8C03\u67E5\u9898\u76EE");
        }
        return sq;
    }

    public void setSurveyGroup(String sur_id, String group_id)
        throws BusinessException
    {
        String sql = "insert into t_group_survey (survey_id,group_id) values ('" + sur_id + "','" + group_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8C03\u67E5\u95EE\u5377\u76F8\u5173\u5C97\u4F4D\u3001\u804C\u52A1\u65F6\u51FA\u9519!");
        }
    }

    public void setSurveyTrain(String sur_id, String train_id)
        throws BusinessException
    {
        String sql = "insert into t_train_survey (survey_id,train_id) values ('" + sur_id + "','" + train_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8C03\u67E5\u95EE\u5377\u76F8\u5173\u57F9\u8BAD\u73ED\u65F6\u51FA\u9519!");
        }
    }

    public void setSurveyOrganize(String sur_id, String organize_id)
        throws BusinessException
    {
        String sql = "insert into t_organize_survey(survey_id,organize_id) values ('" + sur_id + "','" + organize_id + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BBE\u7F6E\u8C03\u67E5\u95EE\u5377\u76F8\u5173\u7EC4\u7EC7\u7ED3\u6784\u65F6\u51FA\u9519!");
        }
    }

    public void addSurveyQuestion(SurveyQuestion sq)
        throws BusinessException
    {
        try
        {
            hbpservice.store(sq);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6DFB\u52A0\u8C03\u67E5\u95EE\u5377\u9898\u76EE\u65F6\u51FA\u9519!");
        }
    }

    public List findSurveyQuestion(String id)
        throws BusinessException
    {
        List list = null;
        String hql = "from SurveyQuestion sq where sq.surveyId='" + id + "'";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636E\u95EE\u5377ID\u67E5\u8BE2\u8C03\u67E5\u9898\u76EE\u65F6\u51FA\u9519!");
        }
        return list;
    }

    public List surveyManage(String user_id, String currentTime)
        throws BusinessException
    {
        List list = null;
        String hql = "from Survey sur where sur.creater='" + user_id + "' and sur.date>='" + currentTime + "'";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5F53\u524D\u8C03\u67E5\u95EE\u5377\u5217\u8868\u65F6\u51FA\u9519!");
        }
        return list;
    }

    public List surveyLast(String user_id, String currentTime)
        throws BusinessException
    {
        List list = null;
        String hql = "from Survey sur where sur.creater='" + user_id + "' and sur.date<'" + currentTime + "'";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5F53\u524D\u8C03\u67E5\u95EE\u5377\u5217\u8868\u65F6\u51FA\u9519!");
        }
        return list;
    }

    public List findCurrentSurvey(String user_id, String organize_id, String current_time)
        throws BusinessException
    {
        final String userId = user_id;
        final String organizeId = organize_id;
        final String currentTime = current_time;
        List list = null;
        CallableStatementCallback cb = new CallableStatementCallback() {

            public Object doInCallableStatement(CallableStatement cs)
                throws SQLException, DataAccessException
            {
                List list = new LinkedList();
                cs.setString(1, userId);
                cs.setString(2, organizeId);
                cs.setString(3, currentTime);
                cs.execute();
                ResultSet rs;
                Map map;
                for(rs = cs.getResultSet(); rs.next(); list.add(map))
                {
                    map = new HashMap();
                    map.put("id", rs.getString("id"));
                    map.put("name", rs.getString("name"));
                    map.put("type", rs.getString("type"));
                    SurveyServiceImp.logger.info("type:" + rs.getString("type"));
                    map.put("organize_id", rs.getString("organize_id"));
                    map.put("oname", rs.getString("oname"));
                    map.put("date", rs.getString("date"));
                    map.put("release_time", rs.getString("release_time"));
                    map.put("uid", rs.getString("uid"));
                    map.put("uname", rs.getString("uname"));
                }

                rs.close();
                return list;
            }

        };
        try
        {
            list = (List)jdbcpservice.getSpringJdbcTemplate().execute("{call pro_user_survey(?,?,?)}", cb);
        }
        catch(DataAccessException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5B66\u5458\u67E5\u770B\u5F53\u524D\u8C03\u67E5\u95EE\u5377\u5217\u8868\u65F6\u51FA\u9519: " + e.getMessage());
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5B66\u5458\u67E5\u770B\u5F53\u524D\u8C03\u67E5\u95EE\u5377\u5217\u8868\u65F6\u51FA\u9519: " + e.getMessage());
        }
        return list;
    }

    public void answerSurvey(String uid, String qid, String answer)
        throws BusinessException
    {
        String sql = "insert into t_user_survey (user_id,question_id,user_answer) values ('" + uid + "','" + qid + "','" + answer + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5B66\u5458\u56DE\u7B54\u8C03\u67E5\u95EE\u5377\u9898\u76EE\u65F6\u51FA\u9519!");
        }
    }

    public List surveyResult(String id)
        throws BusinessException
    {
        List list = null;
        List reList = new LinkedList();
        String sql = "select sq.question as question, sq.answer as answer, sq.id as qid, s.count as total from t_survey s inner join t_survey_question sq on sq.survey_id = s.id and s.id='" + id + "' " + "group by sq.question,sq.answer,sq.id,s.count";
        try
        {
            list = jdbcpservice.query(sql);
            for(int i = 0; i < list.size(); i++)
            {
                String count = "";
                Map map = new HashMap();
                String question_id = (String)((Map)list.get(i)).get("qid");
                String answer = (String)((Map)list.get(i)).get("answer");
                String user_answer[] = answer.split(";;;");
                map.put("question", (String)((Map)list.get(i)).get("question"));
                map.put("answer", answer);
                map.put("question_id", question_id);
                map.put("total", (Integer)((Map)list.get(i)).get("total"));
                for(int j = 0; j < user_answer.length; j++)
                {
                    HashMap choice_hm = new HashMap();
                    choice_hm.put("0", "A");
                    choice_hm.put("1", "B");
                    choice_hm.put("2", "C");
                    choice_hm.put("3", "D");
                    choice_hm.put("4", "E");
                    choice_hm.put("5", "F");
                    String sql1 = "select count(user_answer) as count,user_answer from t_user_survey where question_id = '" + question_id + "' and user_answer='" + choice_hm.get((new StringBuffer(String.valueOf(j))).toString()) + "' group by user_answer";
                    List list1 = jdbcpservice.query(sql1);
                    if(list1 == null || list1.size() <= 0 || list1.get(0).equals(new Integer(0)))
                        count = count + "0;;;";
                    else
                        count = count + ((Map)list1.get(0)).get("count") + ";;;";
                }

                map.put("count", count);
                reList.add(map);
            }

        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u770B\u8C03\u67E5\u95EE\u5377\u7ED3\u679C\u51FA\u9519!");
        }
        return reList;
    }

    public void countSurvey(String id)
        throws BusinessException
    {
        Survey survey = findSurveyById(id);
        int count = survey.getCount().intValue();
        survey.setCount(new Integer(count + 1));
        save(survey);
    }

    private static Logger logger;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */
    static Class class$2; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.survey.SurveyServiceImp.class.getName());
    }

}
